{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<center><img src=\"./image/CLBLOGO.jpg\" alt=\"创乐博\" style=\"width: 300px;\"/></center>\n",
    "\n",
    "# 1.蜂鸣器按键实验\n",
    "\n",
    "@－－－－湖南创乐博智能科技有限公司－－－－<br>\n",
    "@  文件名：1.蜂鸣器按键实验.py <br>\n",
    "@  版本：V2.0 <br>\n",
    "@  author: zhulin<br>\n",
    "@  说明：无源蜂鸣器实验\n",
    "我们一起来学习一下无源蜂鸣器实验吧，播放音乐！<br>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "    Playing song 1...\n",
      "\n",
      "\n",
      "    Playing song 2...\n",
      "\n",
      "    Playing song 1...\n",
      "\n",
      "\n",
      "    Playing song 2...\n",
      "\n",
      "    Playing song 1...\n",
      "\n",
      "\n",
      "    Playing song 2...\n"
     ]
    }
   ],
   "source": [
    "import RPi.GPIO as GPIO\n",
    "import time\n",
    "\n",
    "Buzzer = 11\n",
    "\n",
    "CL = [0, 131, 147, 165, 175, 196, 211, 248]\t\t# Frequency of Low C notes\n",
    "CM = [0, 262, 294, 330, 350, 393, 441, 495]\t\t# Frequency of Middle C notes\n",
    "CH = [0, 525, 589, 661, 700, 786, 882, 990]\t\t# Frequency of High C notes\n",
    "song_1 = [\tCM[3], CM[5], CM[6], CM[3], CM[2], CM[3], CM[5], CM[6], # Notes of song1\n",
    "\t\t\tCH[1], CM[6], CM[5], CM[1], CM[3], CM[2], CM[2], CM[3], \n",
    "\t\t\tCM[5], CM[2], CM[3], CM[3], CL[6], CL[6], CL[6], CM[1],\n",
    "\t\t\tCM[2], CM[3], CM[2], CL[7], CL[6], CM[1], CL[5]\t]\n",
    "\n",
    "beat_1 = [\t1, 1, 3, 1, 1, 3, 1, 1, \t\t\t# Beats of song 1, 1 means 1/8 beats\n",
    "\t\t\t1, 1, 1, 1, 1, 1, 3, 1, \n",
    "\t\t\t1, 3, 1, 1, 1, 1, 1, 1, \n",
    "\t\t\t1, 2, 1, 1, 1, 1, 1, 1, \n",
    "\t\t\t1, 1, 3\t]\n",
    "\n",
    "song_2 = [\tCM[1], CM[1], CM[1], CL[5], CM[3], CM[3], CM[3], CM[1], # Notes of song2\n",
    "\t\t\tCM[1], CM[3], CM[5], CM[5], CM[4], CM[3], CM[2], CM[2], \n",
    "\t\t\tCM[3], CM[4], CM[4], CM[3], CM[2], CM[3], CM[1], CM[1], \n",
    "\t\t\tCM[3], CM[2], CL[5], CL[7], CM[2], CM[1]\t]\n",
    "\n",
    "beat_2 = [\t1, 1, 2, 2, 1, 1, 2, 2, \t\t\t# Beats of song 2, 1 means 1/8 beats\n",
    "\t\t\t1, 1, 2, 2, 1, 1, 3, 1, \n",
    "\t\t\t1, 2, 2, 1, 1, 2, 2, 1, \n",
    "\t\t\t1, 2, 2, 1, 1, 3 ]\n",
    "\n",
    "def setup():\n",
    "    GPIO.setwarnings(False)\n",
    "    GPIO.setmode(GPIO.BOARD)\t\t# Numbers GPIOs by physical location\n",
    "    GPIO.setup(Buzzer, GPIO.OUT)\t# Set pins' mode is output\n",
    "    global Buzz\t\t\t\t\t\t# Assign a global variable to replace GPIO.PWM\n",
    "    Buzz = GPIO.PWM(Buzzer, 440)\t# 440 is initial frequency.\n",
    "    Buzz.start(50)\t\t\t\t\t# Start Buzzer pin with 50% duty ration\n",
    "\n",
    "def loop():\n",
    "\twhile True:\n",
    "\t\tprint('\\n    Playing song 1...')\n",
    "\t\tfor i in range(1, len(song_1)):\t\t# Play song 1\n",
    "\t\t\tBuzz.ChangeFrequency(song_1[i])\t# Change the frequency along the song note\n",
    "\t\t\ttime.sleep(beat_1[i] * 0.5)\t\t# delay a note for beat * 0.5s\n",
    "\t\ttime.sleep(1)\t\t\t\t\t\t# Wait a second for next song.\n",
    "\n",
    "\t\tprint('\\n\\n    Playing song 2...')\n",
    "\t\tfor i in range(1, len(song_2)):     # Play song 1\n",
    "\t\t\tBuzz.ChangeFrequency(song_2[i]) # Change the frequency along the song note\n",
    "\t\t\ttime.sleep(beat_2[i] * 0.5)     # delay a note for beat * 0.5s\n",
    "\n",
    "def destory():\n",
    "\tBuzz.stop()\t\t\t\t\t# Stop the buzzer\n",
    "\tGPIO.output(Buzzer, 1)\t\t# Set Buzzer pin to High\n",
    "\tGPIO.cleanup()\t\t\t\t# Release resource\n",
    "\n",
    "if __name__ == '__main__':\t\t# Program start from here\n",
    "\tsetup()\n",
    "\ttry:\n",
    "\t\tloop()\n",
    "\texcept KeyboardInterrupt:  \t# When 'Ctrl+C' is pressed, the child program destroy() will be  executed.\n",
    "\t\tdestory()\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
